package org.shopen.ai.puremvc.view.project
{
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.utilities.statemachine.StateMachine;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.model.project.vo.Project;
	import org.shopen.ai.puremvc.view.common.AdvControlBarMediator;
	import org.shopen.ai.puremvc.view.project.components.ProjectSearchView;
	import org.shopen.ai.puremvc.view.project.components.ProjectSetupView;
	import org.shopen.ai.puremvc.view.project.components.ProjectStatisticView;
	import org.shopen.ai.puremvc.view.project.components.ProjectWindow;

	public class ProjectWindowMediator extends Mediator
	{
		public static const NAME : String = "ProjectWindowMediator";
		
		public function ProjectWindowMediator(viewComponent:Object=null)
		{
			super(ProjectWindowMediator.NAME, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return ProjectWindowMediator.NAME;
		}
		
		protected function get projectWindow() : ProjectWindow
		{
			var result : ProjectWindow = viewComponent as ProjectWindow;
			return result;
		}
		
		override public function onRegister():void
		{
			facade.registerMediator(new StepBoxMediator(projectWindow.stepBox));
			facade.registerMediator(new AdvControlBarMediator(projectWindow.headerControlBar, ".header"));
			facade.registerMediator(new AdvControlBarMediator(projectWindow.footerControlBar, ".footer"));
			sendNotification(ApplicationConstants.INITIALIZE_PROJECT);
			//facade.registerMediator(new ProjectPreviewMediator(projectWindow.projectPreview));
			addEventListeners();
		}
		
		private function addEventListeners() : void
		{
			projectWindow.addEventListener(ProjectWindow.PREVIEW_CREATED, onPreViewCreated);
			projectWindow.addEventListener(ProjectWindow.SETUPVIEW_CREATED, onSetupViewCreated);
			projectWindow.addEventListener(ProjectWindow.SEARCHVIEW_CREATED, onSearchViewCreated);
			projectWindow.addEventListener(ProjectWindow.STATVIEW_CREATED, onStatViewCreated);
		}
		
		private function onPreViewCreated(event : Event) : void
		{
			 checkForMediator(ProjectWindow.PREVIEW, projectWindow.projectPreView);
		}
		
		private function onSetupViewCreated(event : Event) : void
		{
			checkForMediator(ProjectWindow.SETUP, projectWindow.projectSetupView);
		}
		
		private function onSearchViewCreated(event : Event) : void
		{
			checkForMediator(ProjectWindow.SEARCH, projectWindow.projectSearchView);
		}
		
		private function onStatViewCreated(event : Event) : void
		{
			checkForMediator(ProjectWindow.STATISTIC, projectWindow.projectStatView);
		}
		
		protected function checkForMediator(childSelector : int, child : Object):void
        {
            switch (childSelector)
            {
            	case ProjectWindow.PREVIEW:
            		if (!facade.hasMediator(ProjectPreViewMediator.NAME))
            			facade.registerMediator(new ProjectPreViewMediator(child));
            		break;
            	case ProjectWindow.SETUP:
            		if (!facade.hasMediator(ProjectSetupViewMediator.NAME))
            			facade.registerMediator(new ProjectSetupViewMediator(child as ProjectSetupView)); 
            		break;
            	case ProjectWindow.SEARCH:
            		if (!facade.hasMediator(ProjectSearchViewMediator.NAME))
            			facade.registerMediator(new ProjectSearchViewMediator(child as ProjectSearchView));
            		break;
            	case ProjectWindow.STATISTIC:
            		if (!facade.hasMediator(ProjectStatisticViewMediator.NAME))
            			facade.registerMediator(new ProjectStatisticViewMediator(child as ProjectStatisticView));
            		break;
            }
        }
		
		override public function listNotificationInterests():Array
		{
			return [StateMachine.CHANGED, ApplicationConstants.PROJECT_INITIALIZED];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case StateMachine.CHANGED:
					handleStateChange(notification);
					break;
				case ApplicationConstants.PROJECT_INITIALIZED:
					var project : Project = notification.getBody() as Project;
					projectWindow.projectTitle = project.meta.title;
					break;
			}
		}
		
		private function handleStateChange(notification : INotification) : void
		{
			var type : String = notification.getType();
			switch (type)
			{
				case ApplicationConstants.STATE_PROJECT_PREVIEW:
					projectWindow.selectedStep = ProjectWindow.PREVIEW;
					sendNotification(ApplicationConstants.CHANGE_STEP, ProjectWindow.PREVIEW);
					break;
				case ApplicationConstants.STATE_PROJECT_SETUP:
					projectWindow.selectedStep = ProjectWindow.SETUP;
					sendNotification(ApplicationConstants.CHANGE_STEP, ProjectWindow.SETUP);
					break;
				case ApplicationConstants.STATE_PROJECT_SEARCHING:
					projectWindow.selectedStep = ProjectWindow.SEARCH;
					sendNotification(ApplicationConstants.CHANGE_STEP, ProjectWindow.SEARCH);
					break;
				case ApplicationConstants.STATE_PROJECT_STATISTIC:
					projectWindow.selectedStep = ProjectWindow.STATISTIC;
					sendNotification(ApplicationConstants.CHANGE_STEP, ProjectWindow.STATISTIC);
					break;
			}
		}
		
	}
}